﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Globalization;

namespace AppFuscator
{
    internal class User
    {
        public double Balance { get; set; }
        public string Name { get; set; }
        public string Currency { get; set; }
        public AccountMode Mode { get; set; }
        public bool IsLimited { get; set; }
        public UserTariff Tariff = UserTariff.Instance;

        private static volatile User instance;
        private static object syncRoot = new object();
        public static User Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new User();
                    }
                }

                return instance;
            }
        }

        public void LoadFromAnswer(string answer)
        {
            JObject data = Utils.ParseJson(answer);
            if (data == null)
                return;
            if (data.Property("state").Value.ToString() != "ok")
                return;

            Tariff.LoadFromAnswer(answer);

            Name = data.Property("email").Value.ToString();
            Balance = double.Parse(data.Property("balance").Value.ToString(), CultureInfo.InvariantCulture);
            Currency = data.Property("currency").Value.ToString();
            Mode = AccountModeConverter.Convert(data.Property("mode").Value.ToString().ToLower());
            IsLimited = data.Property("isLimited").Value.ToString().ToLower() == "true";
        }
    }
}